<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<div :style="{width: '70px'}"><label>创建时间从:</label></div>
				<el-date-picker v-model="date.createTimeFrom" type="date" @change="dateFilter"></el-date-picker>
				<div :style="{width: '50px','padding-left': '20px'}"><label>至:</label></div>
				<el-date-picker v-model="date.createTimeTo" type="date" @change="dateFilter"></el-date-picker>
			</div>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add">添加</el-button>
				<sc-file-export :apiObj="$API.factory.customer.download" fileName="人员列表(异步)" blob>
					<template #default="{open}">
						<el-button type="primary" icon="sc-icon-download" @click="open">导出(异步)</el-button>
					</template>
				</sc-file-export>
				<!--				<el-button type="danger" plain icon="el-icon-delete"></el-button>-->
			</div>
			<div>
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="关键词"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
					<el-button type="success" icon="el-icon-delete" @click="reset">清空</el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe>
				<!--				<el-table-column type="selection" width="50"></el-table-column>-->
				<el-table-column label="姓名" prop="name" width="150"></el-table-column>
				<el-table-column label="地址" prop="address" width="280"></el-table-column>
				<el-table-column label="联系方式" prop="contact" width="150"></el-table-column>
				<el-table-column label="创建时间" prop="createDate" width="150"/>
				<el-table-column label="更新时间" prop="lastUpdateDate" width="150"/>
				<el-table-column label="操作" fixed="right" align="right" width="280">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row)">编辑</el-button>
							<el-button text type="primary" size="small" @click="delete_row(scope.row)">删除</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess"
				 @closed="dialog.save=false"></save-dialog>

</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import saveDialog from './save'
import scFileExport from "@/components/scFileExport";

export default {
	name: 'customer',
	components: {
		scSelectFilter,
		saveDialog,
		scFileExport
	},
	data() {
		return {
			dialog: {
				save: false
			},
			date: {
				createTimeFrom: {},
				createTimeTo: {},
			},
			list: {
				apiObj: this.$API.factory.customer.page
			},
			search: {
				keyword: ""
			}
		}
	},
	methods: {
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//日期搜索
		dateFilter() {
			this.$refs.table.upData({
				createTimeFrom: this.$TOOL.dateFormat(this.date.createTimeFrom, "yyyy-MM-dd"),
				createTimeTo: this.$TOOL.dateFormat(this.date.createTimeTo, "yyyy-MM-dd"),
			})
		},
		// 重置搜索条件
		reset() {
			this.search.keyword = "";
			this.date.createTimeFrom = {};
			this.date.createTimeTo = {};
			// 重载数据，只保留
			this.$refs.table.reload();
		},//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		},
		//窗口编辑
		async table_edit(row) {
			this.dialog.save = true
			this.$TOOL.response(await this.$API.factory.customer.detail.get(row.id),
				(data) => {
					this.$nextTick(() => {
						this.$refs.saveDialog.open('edit').setData(data)
					})
				})
		},
		//窗口新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//查看
		async table_show(row) {
			this.dialog.save = true
			this.$TOOL.response(await this.$API.factory.customer.detail.get(row.id),
				(data) => {
					this.$nextTick(() => {
						this.$refs.saveDialog.open('show').setData(data)
					})
				},(res)=>{
					this.$alert(res.message, "错误", {type: 'error'})
				})
		},
		//删除
		async delete_row(row) {
			this.$confirm(`确定删除${row.name}吗？`, '提示', {
				type: 'warning'
			}).then(async ()=>{
				// 删除
				this.$TOOL.response(await this.$API.factory.customer.delete.delete(row.id),
					() => {
						// 刷新页面
						this.$refs.table.refresh()
						this.$message.success("删除成功")
					},(res)=>{
						this.$alert(res.message, "提示", {type: 'error'})
					})
			}).catch(()=>{
				this.$message.error("处理异常")
			})

		}
	}
}
</script>

<style>
</style>
